package valid_contr
{
	import flash.events.*;
	import flash.geom.*;
	
	import mx.controls.*;
	import mx.events.*;
	import mx.managers.*;
	import mx.validators.*;

	public class string_textinput extends TextInput
	{
		private var validatorobj:StringValidator = new StringValidator
		//private var errorTip:ToolTip
		private var errorTip:Text;
		private var tip_position:String

		
		public function string_textinput()
		{
			super();
			validatorobj.source = this;
			validatorobj.listener = undefined;
			validatorobj.property = "text";
			//this.addEventListener(MouseEvent.CLICK, clearvalidation);
			this.addEventListener(FocusEvent.FOCUS_OUT, validate2);
			
		}
		
		private function validate2(e:Event):void{
			validate()
		}
		
		public function validate():Boolean
		{
			if (validatorobj.validate(null,true).type == ValidationResultEvent.INVALID){
			
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
				var pt:Point = new Point(0, 0);
        		pt = this.localToGlobal(pt);
				this.errorString = validatorobj.validate(null,true).message
				switch(tipposition){
					case "right":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y,"errorTipRight") as ToolTip;
							errorTip = new Text();
							errorTip.text = validatorobj.validate(null,true).message;
							//errorTip.setStyle('fontSize',"14px");
							errorTip.setStyle('color','Red');
							this.parent.addChild(errorTip);
							errorTip.x = this.x + this.width + 5;
							errorTip.y = this.y
							break;
					case "top":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y - 45,"errorTipAbove") as ToolTip;
							break;
					case "bottom":
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x+5,pt.y + this.height + 5,"errorTipBelow") as ToolTip;
							break;
					default:
							//errorTip = ToolTipManager.createToolTip(validatorobj.validate(null,true).message,pt.x + this.width + 5,pt.y) as ToolTip;
							errorTip = new Text();
							errorTip.text = validatorobj.validate(null,true).message;
							//errorTip.setStyle('fontSize',"14px");
							errorTip.setStyle('color','Red');
							this.parent.addChild(errorTip);
							errorTip.x = this.x + this.width + 5;
							errorTip.y = this.y
				}
				errorTip.setStyle("styleName", "errorTip");

			return false
			}
			else
			{
				try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
			}
			return true
		}
		
		public function clearvalidation(e:Event):void{
			this.errorString = '';
			try{
				//ToolTipManager.destroyToolTip(errorTip);
				this.parent.removeChild(errorTip);
			}
			catch(ex:Object){}
			
		}
		
		public function get required():Boolean
		{
			return validatorobj.required;
		}

		public function set required(bool:Boolean):void
		{
			validatorobj.required = bool;
		}
		
		public function get minlength():String
		{
			return validatorobj.minLength.toString();
		}

		public function set minlength(length:String):void
		{
			validatorobj.minLength = length;
		}
		
		public function get maxlength():String
		{
			return validatorobj.maxLength.toString();
		}

		public function set maxlength(length:String):void
		{
			validatorobj.maxLength = length;
		}
		
		public function get tipposition():String
		{
			return tip_position;
		}

		public function set tipposition(tipposition2:String):void
		{
			tip_position = tipposition2;
		}
		
		override public function set text(s:String):void{
			super.text = s;
			validate();
		}
		
	}
}